/**
 * 
 */
package systole.persistence.brokersDB;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import systole.domain.clinicalInformation.ClinicalInformation;
import systole.domain.persons.Patient;
import systole.exceptions.ExceptionDAO;
import systole.persistence.FacadeDB;
import systole.persistence.brokersInterface.ClinicalInfoBroker;
import systole.view.messages.ErrorMessages;

/**
 * @author jmj
 *
 */
public class ClinicalInfoBrokerDB extends BrokerDB implements
        ClinicalInfoBroker {

    /**
     * 
     */
    public ClinicalInfoBrokerDB() {
        super();
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ClinicalInfoBroker#getAllPatientClinicalInformation(systole.domain.persons.Patient)
     */
    @Override
    public List<ClinicalInformation> getAllPatientClinicalInformation(
            Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting all patient clinical information");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            @SuppressWarnings("unchecked")
            List<ClinicalInformation> list = currentSession.createCriteria(ClinicalInformation.class).add(Restrictions.eq("patient", patient)).list();
            this.logger.logDebug("get patient clinical information");
            return list;
        } catch (HibernateException e) {
            this.logger.logError("error on get all patient clinical information, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la información clínica del paciente", e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ClinicalInfoBroker#insert(systole.domain.clinicalInformation.ClinicalInformation)
     */
    @Override
    public void insert(ClinicalInformation clinicalInformation)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("saving patient clinical information");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.save(clinicalInformation);
            this.logger.logDebug("saved patient clinical information");
        } catch (HibernateException e) {
            this.logger.logError("error on save patient clinical information, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    /* (non-Javadoc)
     * @see systole.persistence.brokersInterface.ClinicalInfoBroker#update(systole.domain.clinicalInformation.ClinicalInformation)
     */
    @Override
    public void update(ClinicalInformation clinicalInformation)
            throws ExceptionDAO {
        try {
            this.logger.logDebug("updating patient clinical information");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.update(clinicalInformation);
            this.logger.logDebug("updated patient clinical information");
        } catch (HibernateException e) {
            this.logger.logError("error on update patient clinical information, msg: " + e.getMessage());
            throw new ExceptionDAO(ErrorMessages.CHANGES_NOT_SAVE, e.fillInStackTrace());
        }
    }

    public void delete(ClinicalInformation clinicalInformation) throws ExceptionDAO {
        try {
            this.logger.logDebug("deleting patient clinical information");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            currentSession.delete(clinicalInformation);
            this.logger.logDebug("deleted patient clinical information");
        } catch (HibernateException e) {
            this.logger.logError("error on delete patient clinical information, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo eliminar", e.fillInStackTrace());
        }
    }

    public ClinicalInformation getLastClinicalInfoByPatient(Patient patient) throws ExceptionDAO {
        try {
            this.logger.logDebug("Getting last clinical information by patient");
            Session currentSession = FacadeDB.getInstance().getCurrentSession();
            ClinicalInformation clinicalInfo = (ClinicalInformation) currentSession.
                    createCriteria(ClinicalInformation.class).add(Restrictions.eq("patient", patient)).
                    addOrder(Order.desc("informationDate")).setMaxResults(1).uniqueResult();
            this.logger.logDebug("get last clinical information patient");
            return clinicalInfo;
        } catch (HibernateException e) {
            this.logger.logError("error on get last clinical information patient, msg: " + e.getMessage());
            throw new ExceptionDAO("No se pudo obtener la información clínica del paciente", e.fillInStackTrace());
        }
    }
}
